package com.study.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.study.bean.Address;
import com.study.bean.Details;
import com.study.bean.Orders;
import com.study.bean.Photo;
import com.study.bean.Shoppincat;
import com.study.bean.Xiangqing;
import com.study.biz.Photobiz;
import com.study.biz.Subbiz;
import com.study.biz.Svmidbiz;
import com.study.dao.Addressjsdao;
import com.study.dao.Shoppincatdao;
import com.study.dao.Xiangqingdao;

/**
 * Servlet implementation class jiesuanservlet
 */
@WebServlet("/jiesuan")
public class jiesuanservlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	Addressjsdao addressdao=new Addressjsdao();    
	Shoppincatdao shoppincatdao=new Shoppincatdao();
	Xiangqingdao xiangqingdao=new Xiangqingdao();
	@Override
	public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Object id=session.getAttribute("uid");
    	int ids=Integer.parseInt(id.toString());     		
    	List<Address> addresslist=addressdao.yonghuById(ids);

    	request.setAttribute("addresslist",addresslist);
    	System.out.println(addresslist+"$$#####");    	
    	//通过用户id获取选中购物信息    	


    	
    	//通过用户id获取选中购物信息    	

    	String sid=request.getParameter("sid");	
    	request.setAttribute("sid", sid);
    	String sccount =request.getParameter("sccount");
    	request.setAttribute("sccount", sccount);
    	String[] sidarr=sid.split(",");
    	String[] sccountarr=sccount.split(",");
    	List<Shoppincat> xiangqinglist=new ArrayList<>();
    	for(int i=0;i<sidarr.length;i++){
    	Shoppincat xiangqing=shoppincatdao.sidById(Integer.parseInt(sidarr[i]));
    	xiangqing.setScount(Integer.parseInt(sccountarr[i]));	
    	xiangqinglist.add(xiangqing);
    	System.out.println(sidarr[i]+"#####################33");
    

    	}
    	request.setAttribute("xiangqinglist", xiangqinglist);
    	
		//购物车跳转结算

		return "f:jiesuan";
	}
    public String huoqudizhi(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	Object id=session.getAttribute("uid");
    	int ids=Integer.parseInt(id.toString());
    	
    	String sid=request.getParameter("sid");	
    	String sccount =request.getParameter("sccount");
    	
    	String addname=request.getParameter("addname");			//鍚嶅瓧
    	String provice=request.getParameter("provice");			//鐪�
    	String city=request.getParameter("city");				//甯�
    	String district=request.getParameter("district");		//鍘�
    	String detailedaddress=request.getParameter("detailedaddress");		//璇︾粏鍦板潃
    	String addphone=request.getParameter("addphone");		//鎵嬫満鍙�
    	String addisdefault=request.getParameter("addisdefault");  	//鏄惁榛樿

    	String addrssid=request.getParameter("dzbtnbc");		//閫氳繃涓婚敭鏉ュ垽鏂柊澧炴垨淇敼
    	

    	int addisdefaults=1;
    	if(addisdefault!=null){
    		addisdefaults=0;
    		Addressjsdao tjaddressdao=new Addressjsdao();
    		tjaddressdao.qbianUpdate();
    		System.out.println("杩涘叆寮哄埗鍙樺舰鍒ゆ柇");
    	}else{ 
    		addisdefaults=1;
    	}
	
    	Addressjsdao tjaddressdao=new Addressjsdao();
    	if(addrssid!=null&&!addrssid.equals("")){
    		System.out.println(ids);
    		Address xg=new Address();
    		xg.setUserid(ids);
    		xg.setAddname(addname);
    		xg.setProvince(provice);
    		xg.setCity(city);
    		xg.setCounty(district);
    		xg.setDetailedaddress(detailedaddress);
    		xg.setAddphone(addphone);
    		xg.setAddisdefault(addisdefaults);
    		Boolean open=tjaddressdao.dizhiUpdate(xg, Integer.parseInt(addrssid));
    	}else{
    		Address hqaddresslist=new Address(null,ids,addname,provice,city,district,detailedaddress,addphone,addisdefaults);
    		
    		tjaddressdao.doInsert(hqaddresslist);
    	}
    		return "r:jiesuan?sid="+sid+"&sccount="+sccount;  	
    } 
    
    public String xiugai(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	response.setContentType("application/json;charset=utf-8");

    	System.out.println("修改");

    	String addrssid=request.getParameter("addrssid");		//涓婚敭
    	String xz=request.getParameter("xz");		//涓婚敭
    	System.out.println(xz+"xxxxxxxxxxxx");
    	int addrssids=Integer.parseInt(addrssid);
    	System.out.println(addrssids);
    	Addressjsdao dzaddressdao=new Addressjsdao();   	
    	Address dzaddresslist=dzaddressdao.dizhiById(addrssids);
    	System.out.println(JSON.toJSONString(dzaddresslist));
    	/*PrintWriter out=response.getWriter();
    	out.println(JSON.toJSONString(dzaddresslist));*/
		return JSON.toJSONString(dzaddresslist);
    	
    }
    public String shanchu(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	response.setContentType("application/json;charset=utf-8");

    	String addrssid=request.getParameter("addrssid");		//涓婚敭

    	int addrssids=Integer.parseInt(addrssid);

    	System.out.println("鍒犻櫎");
    	if(addrssid!=null){
    		Addressjsdao addressdao=new Addressjsdao();   
    		Address dzaddresslist=addressdao.dizhiById(addrssids);
    		addressdao.dizhidelete(addrssids);
        	return "{\"res\":true}";
    	}   	
    	return "{\"res\":false}";	
    }
    public String shezhi(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	response.setContentType("application/json;charset=utf-8");

    	String addrssid=request.getParameter("addrssid");		//涓婚敭
    	int addrssids=Integer.parseInt(addrssid);
    	System.out.println("璁剧疆鍦板潃");
    	if(addrssid!=null){
    		Addressjsdao tjaddressdao=new Addressjsdao();
    		tjaddressdao.qbianUpdate();
    		tjaddressdao.zbianUpdate(addrssids);
        	return "{\"res\":true}";
    	}	
    	return "{\"res\":false}";	
    }
    
    public String xiangxiang(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	response.setContentType("application/json;charset=utf-8");

    	System.out.println("鎴戝啀鎯虫兂");	
    	String orid=request.getParameter("orid");	//鍗曞彿
    	Object id=session.getAttribute("uid");
    	int ids=Integer.parseInt(id.toString());	//鐢ㄦ埛id
    	String orstatus="待支付"	;		//鏀粯鐘舵��
    	String orpayment="未支付";		//鏀粯鏂瑰紡	
    	String price=request.getParameter("price");		//鏀粯鏃堕噾棰�
    	String oruser=request.getParameter("oruser");	//收货人
    	String orphone=request.getParameter("orphone");	//收货电话
    	String ordizhi=request.getParameter("ordizhi");	//收货地址
    	System.out.println(ordizhi+"####################33"+oruser);
    	Orders orders=new Orders(orid,ids,orstatus,orpayment,new BigDecimal(price),new Date(System.currentTimeMillis()),oruser,orphone,ordizhi);
    	Shoppincatdao shoppincatdao=new Shoppincatdao();

    	shoppincatdao.dingdanInsert(orders);		//鎻掑叆璁㈠崟琛�
    	
    	
    	String sid=request.getParameter("sid");				
    	String sccount =request.getParameter("sccount");
    	String[] sidarr=sid.split(",");
    	String[] sccountarr=sccount.split(",");
    	for(int i=0;i<sidarr.length;i++){
    		Shoppincat xiangqing=shoppincatdao.sidById(Integer.parseInt(sidarr[i]));
        	BigDecimal jiage=xiangqing.getSprice();
    		Details details=new Details(null,orid,Integer.parseInt(sidarr[i]),jiage,Integer.parseInt(sccountarr[i]));		//鐢熸垚璁㈠崟璇︽儏>>>>>>> branch 'cwj' of https://gitee.com/liaowenbo0210/Electronic.git
	        shoppincatdao.dingdanxqInsert(details);
	        shoppincatdao.scountUpdate(Integer.parseInt(sccountarr[i]), Integer.parseInt(sidarr[i]));
	        shoppincatdao.dingdandelete(ids, Integer.parseInt(sidarr[i]));
    	}

		return  "{\"res\":true}";   	
    }
    public String fukuan(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	response.setContentType("application/json;charset=utf-8");	
    	System.out.println("鎴戝啀鎯虫兂");	
    	String orid=request.getParameter("orid");	//鍗曞彿
    	Object id=session.getAttribute("uid");
    	if(id==null){
    		return "{\"res\":false}";
    	}
    	int ids=Integer.parseInt(id.toString());	//鐢ㄦ埛id
    	System.out.println("uid:"+id);
    	String orstatus="已支付"	;		//鏀粯鐘舵��
    	String orpayment="微信支付";		//鏀粯鏂瑰紡	
    	String price=request.getParameter("price");		//鏀粯鏃堕噾棰�
    	String oruser=request.getParameter("oruser");	//收货人
    	String orphone=request.getParameter("orphone");	//收货电话
    	String ordizhi=request.getParameter("ordizhi");	//收货地址
    	System.out.println(ordizhi+"####################33"+oruser);
    	Orders orders=new Orders(orid,ids,orstatus,orpayment,new BigDecimal(price),new Date(System.currentTimeMillis()),oruser,orphone,ordizhi);
    	Shoppincatdao shoppincatdao=new Shoppincatdao();
    	shoppincatdao.dingdanInsert(orders);		//鎻掑叆璁㈠崟琛�
    	
    	String sid=request.getParameter("sid");				
    	String sccount =request.getParameter("sccount");
    	String[] sidarr=sid.split(",");
    	String[] sccountarr=sccount.split(",");
    	for(int i=0;i<sidarr.length;i++){
    		Shoppincat xiangqing=shoppincatdao.sidById(Integer.parseInt(sidarr[i]));
        	BigDecimal jiage=xiangqing.getSprice();
    		Details details=new Details(null,orid,Integer.parseInt(sidarr[i]),jiage,Integer.parseInt(sccountarr[i]));		//鐢熸垚璁㈠崟璇︽儏>>>>>>> branch 'cwj' of https://gitee.com/liaowenbo0210/Electronic.git
	        shoppincatdao.dingdanxqInsert(details);
	        shoppincatdao.scountUpdate(Integer.parseInt(sccountarr[i]), Integer.parseInt(sidarr[i]));
	        shoppincatdao.dingdandelete(ids, Integer.parseInt(sidarr[i]));
    	}

    	
    	return  "{\"res\":true}";  	
    }
}
